//
//  PYSwitcherCell.m
//  IHome
//
//  Created by Jim on 2019/4/6.
//  Copyright © 2019 com.zhujizheng. All rights reserved.
//

#import "PYSwitcherCell.h"

@interface PYSwitcherCell ()

@property (nonatomic, strong) UISwitch *switcher;
@property (nonatomic, weak) id<PYSwitcherCellDelegate> delegate;
@end

@implementation PYSwitcherCell

+ (instancetype)cellWithTableView:(UITableView *)tableView delegate:(nullable id<PYSwitcherCellDelegate>)delegate
{
    PYSwitcherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"switcherCellid"];
    if (!cell) {
        cell = [[PYSwitcherCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"switcherCellid"];
        [cell initSwitcher];
    }
    cell.textLabel.font = PY_Font_Regular(17);
    cell.textLabel.textColor = PY_Dark_Color;
    cell.textLabel.textAlignment = NSTextAlignmentLeft;
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.delegate = delegate;
    return cell;
}

#pragma mark - switch
- (UISwitch *)switcher
{
    if (!_switcher) {
        _switcher = [UISwitch new];
    }
    return _switcher;
}

- (void)initSwitcher
{
    [self.contentView addSubview:self.switcher];
    [self.switcher mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-15);
        make.size.mas_equalTo(CGSizeMake(45, 28));
    }];
    [self.switcher addTarget:self action:@selector(switcherAction:) forControlEvents:UIControlEventValueChanged];
}

- (void)switcherAction:(UISwitch *)switcher
{
    if ([self.delegate respondsToSelector:@selector(switcherCell:valueChanged:)]) {
        [self.delegate switcherCell:self valueChanged:switcher.isOn];
    }
}

@end
